package o2o.web.actions.postman;

import com.alibaba.fastjson.JSONObject;
import o2o.config.dao.UserDaoSql;
import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.JYPageUtils;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.postman.CityPostmanMemory;
import o2o.cache.postman.PostManLikeMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.dao.PO.PojoTables;
import o2o.postman.model.CityPostman;
import o2o.dao.PO.postman.PostManLike;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.VO.postman.CityPostmanVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

//** 用户收藏配送员信息
@RestController
public class PostManLikeAction {

    // 用户添加收藏
    @RequestMapping("/user/postman/like/add")
    public String user_postman_like_add(String token, String postman) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (ValidateUtil.isNull(postman)) {
            return AjaxResult.errorResult("请选择要收藏的骑手");
        }
        CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(postman);
        if (cityPostman == null) {
            return AjaxResult.errorResult("骑手不存在，请核实");
        }
        List<PostManLike> list = PostManLikeMemory.getInstance().listOfPostManLikeByUsername(user.getUsername());
        if (ValidateUtil.isAbsList(list)) {
            for (PostManLike p : list) {
                if (p != null && postman.equals(p.getPostman())) {
                    return AjaxResult.errorResult("您已经收藏过该骑手了");
                }
            }
        }
        long cTime = DateUtils.getCurrTime();
        // 添加骑手啊
        PostManLike p = new PostManLike(user.getUsername(), postman, user.getUsername(), cTime);
        try {
            UserDaoSql.getInstance().insertObj(PojoTables.POSTMAN_LIKE, p);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult("收藏失败，请稍后重试");
        }
        // 添加缓存
        PostManLikeMemory.getInstance().add(cTime);
        return AjaxResult.successResult();
    }


    /**
     * @param token
     * @param postman
     * @return
     * @desc 用户是否手收藏骑手
     */
    @RequestMapping(method = RequestMethod.POST, value = "/user/postman/is-like")
    public String user_postman_is_like(String token, String postman) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (StringUtils.isBlank(postman)) {
            return AjaxResult.errorResult("骑手账号不能为空");
        }
        PostManLike like = PostManLikeMemory.getInstance().getPostManLikeByUsernameAndPostman(user.getUsername(), postman);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", 1);
        if (like == null) {
            jsonObject.put("isLike", 0);
        } else {
            jsonObject.put("isLike", 1);
        }
        return jsonObject.toString();
    }


    // 用户取消收藏骑手
    @RequestMapping("/user/postman/like/cancel")
    public String user_postman_like_del(String token, String postman) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (ValidateUtil.isNull(postman)) {
            return AjaxResult.errorResult("请选择要收藏的骑手");
        }
        if (ValidateUtil.isNull(postman)) {
            return AjaxResult.errorResult("骑手账户不能为空");
        }
        PostManLike p = PostManLikeMemory.getInstance().getPostManLikeByUsernameAndPostman(user.getUsername(), postman);
        if (p == null) {
            return AjaxResult.errorResult("您没有收藏过该骑手，请核实");
        }
        StringBuffer sql = new StringBuffer();
        sql.append("update ").append(PojoTables.POSTMAN_LIKE).append(" set")
                .append(" delFlag = 1 ,updateUser='").append(user.getUsername()).append("' ").append(" where id=").append(p.getId());
        try {
            UserDaoSql.getInstance().executeBySql(sql.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult("取消收藏失败，请稍后重试");
        }
        // 修改缓存
        PostManLikeMemory.getInstance().delete(p.getId());
        return AjaxResult.successResult();
    }


    // 获取用户收藏的骑手信息
    @RequestMapping("/user/postman/like/list")
    public String user_postman_like_list(String token, String city, String key, Integer type, Integer page, Integer size) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (!ValidateUtil.isAbsInteger(page)) {
            page = 1;
        }
        if (!ValidateUtil.isAbsInteger(size)) {
            size = 20;
        }
        List<PostManLike> list = PostManLikeMemory.getInstance().listOfPostManLikeByUsername(user.getUsername());
        if (ValidateUtil.isAbsList(list)) {
            List<String> postManUsernameList = new ArrayList<>();
            List<CityPostman> postManList = new ArrayList<>();
            for (PostManLike p : list) {
                if (p == null || ValidateUtil.isNull(p.getPostman())) {
                    continue;
                }
                CityPostman postman = CityPostmanMemory.getInstance().getPostman(p.getPostman());
                if (postman == null) {
                    continue;
                }
                // 城市
                if (!ValidateUtil.isNull(city)) {
                    if (city.endsWith("市")) {
                        city = city.substring(0, city.lastIndexOf("市"));
                    }
                    if (ValidateUtil.isNull(postman.getCity())) {
                        continue;
                    }
                    if (!city.equals(postman.getCity())) {
                        continue;
                    }
                }
                // 检测骑手类型
                if (type != null && postman.getType() != null && postman.getType() - type != 0) {
                    continue;
                }
                if (!postManUsernameList.contains(p.getPostman())) {
                    postManUsernameList.add(p.getPostman());
                    postManList.add(postman);
                }
            }
            if (ValidateUtil.isAbsList(postManList)) {
                JYPageUtils.PageList pageList = JYPageUtils.getList(postManList, page, size);
                List<CityPostmanVO> postmanVOList = new ArrayList<>();
                for (CityPostman cityPostman : (List<CityPostman>) pageList.list) {
                    CityPostmanVO postmanVO = PostmanMan.getOne(cityPostman);
                    if (null != postmanVO) {
                        postmanVOList.add(postmanVO);
                    }
                }
                if (ValidateUtil.isAbsList(postmanVOList)) {
                    return AjaxResult.successResult(pageList.totalCount, postmanVOList.size(), postmanVOList);
                }
            }
        }
        return AjaxResult.successResult(0, 0, null);
    }

}